package com.ecarx.camera.worker.car.aosp;

import android.car.hardware.CarPropertyConfig;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;

import com.ecarx.camera.utils.LogUtils;

/**
 * CarPropertyManager助手类
 */
public class AospPropertyManagerHelper {

    private static final String TAG = "AospPropertyManagerHelper";

    /**
     * 判断指定propId的功能是否支持
     * @param propId
     * @return
     */
    static boolean isCarPropertySupported(int propId) {
        return getCarPropertyConfig(propId) != null;
    }

    /**
     * 获取CarPropertyConfig
     * @param propId 属性Id
     * @return
     */
    static CarPropertyConfig getCarPropertyConfig(int propId) {
        CarPropertyManager carPropertyManager = AospCarManager.get().getCarPropertyManager();
        if (carPropertyManager == null) {
            LogUtils.w(TAG, "getCarPropertyConfig but carPropertyManager is null");
            return null;
        }
        CarPropertyConfig carPropertyConfig = carPropertyManager.getCarPropertyConfig(propId);
        LogUtils.d(TAG, "getCarPropertyConfig, propId:" + propId + ", carPropertyConfig:" + carPropertyConfig);
        return carPropertyConfig;
    }

    /**
     * 获取指定propId的功能是否可用
     * @param propId
     * @param zone
     * @return
     */
    static boolean isCarPropertyActive(int propId, int zone) {
        CarPropertyManager carPropertyManager = AospCarManager.get().getCarPropertyManager();
        if (carPropertyManager == null) {
            LogUtils.w(TAG, "isCarPropertyActive but carPropertyManager is null");
            return false;
        }
        try {
            return carPropertyManager.isPropertyAvailable(propId, zone);
        } catch (Exception e) {
            LogUtils.w(TAG, "isCarPropertyActive propId:" + propId + ", e:" + e);
        }
        return false;
    }

    /**
     * 获取CarPropertyValue
     * @param propId 属性Id
     * @param areaId 区域Id
     * @param <E>
     * @return
     */
    static <E> CarPropertyValue<E> getCarPropertyValue(int propId, int areaId) {
        CarPropertyManager carPropertyManager = AospCarManager.get().getCarPropertyManager();
        if (carPropertyManager == null) {
            LogUtils.w(TAG, "getCarPropertyValue but carPropertyManager is null");
            return null;
        }
        CarPropertyValue<E> carPropertyValue = carPropertyManager.getProperty(propId, areaId);
        LogUtils.d(TAG, "getCarPropertyValue, propId:" + propId + ", areaId:" + areaId + ", result:" + (carPropertyValue == null ? null : carPropertyValue.getValue()));
        return carPropertyValue;
    }

    /**
     * 设置int属性值
     * @param propId 属性Id
     * @param areaId 区域Id
     * @param val 值
     * @return
     */
    static boolean setCarIntProperty(int propId, int areaId, int val) {
        CarPropertyManager carPropertyManager = AospCarManager.get().getCarPropertyManager();
        if (carPropertyManager == null) {
            LogUtils.w(TAG, "setCarIntProperty but carPropertyManager is null");
            return false;
        }
        LogUtils.d(TAG, "setCarIntProperty, propId:" + propId + ", areaId:" + areaId + ", val:" + val);
        carPropertyManager.setIntProperty(propId, areaId, val);
        return true;
    }

    /**
     * 设置float属性值
     * @param propId 属性Id
     * @param areaId 区域Id
     * @param val 值
     * @return
     */
    static boolean setCarFloatProperty(int propId, int areaId, float val) {
        CarPropertyManager carPropertyManager = AospCarManager.get().getCarPropertyManager();
        if (carPropertyManager == null) {
            LogUtils.w(TAG, "setCarFloatProperty but carPropertyManager is null");
            return false;
        }
        carPropertyManager.setFloatProperty(propId, areaId, val);
        LogUtils.d(TAG, "setCarFloatProperty, propId:" + propId + ", areaId:" + areaId +", val:" + val);
        return true;
    }

    /**
     * 设置boolean属性值
     * @param propId 属性Id
     * @param areaId 区域Id
     * @param val 值
     * @return
     */
    static boolean setCarBooleanProperty(int propId, int areaId, boolean val) {
        CarPropertyManager carPropertyManager = AospCarManager.get().getCarPropertyManager();
        if (carPropertyManager == null) {
            LogUtils.w(TAG, "setCarBooleanProperty but carPropertyManager is null");
            return false;
        }
        carPropertyManager.setBooleanProperty(propId, areaId, val);
        LogUtils.d(TAG, "setCarBooleanProperty, propId:" + propId + ", areaId:" + areaId + ", val:" + val);
        return true;
    }

    /**
     * Register CarPropertyEventCallback to get property updates. Multiple listeners
     * can be registered for a single property or the same listener can be used for different
     * properties. If the same listener is registered again for the same property, it will be
     * updated to new rate.
     * @param carPropertyEventCallback
     * @param propId 属性Id
     * @return
     */
    static boolean registerPropertyEventCallback(CarPropertyManager.CarPropertyEventCallback carPropertyEventCallback, int propId) {
        CarPropertyManager carPropertyManager = AospCarManager.get().getCarPropertyManager();
        if (carPropertyManager == null) {
            LogUtils.w(TAG, "registerPropertyEventCallback but carPropertyManager is null");
            return false;
        }
        boolean result = carPropertyManager.registerCallback(carPropertyEventCallback, propId, CarPropertyManager.SENSOR_RATE_ONCHANGE);
        LogUtils.w(TAG, "registerPropertyEventCallback propId:" + propId + ", result:" + result);
        return result;
    }

    /**
     * Stop getting property update for the given callback. If there are multiple registrations for
     * this callback, all listening will be stopped.
     * @param carPropertyEventCallback
     * @return
     */
    static boolean unregisterPropertyEventCallback(CarPropertyManager.CarPropertyEventCallback carPropertyEventCallback) {
        CarPropertyManager carPropertyManager = AospCarManager.get().getCarPropertyManager();
        if (carPropertyManager == null) {
            LogUtils.w(TAG, "unregisterPropertyEventCallback but carPropertyManager is null");
            return false;
        }
        carPropertyManager.unregisterCallback(carPropertyEventCallback);
        LogUtils.w(TAG, "unregisterPropertyEventCallback");
        return true;
    }
}
